package com.geostar.constructionland.analyse.service.handler.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.geostar.constructionland.common.dao.YzGeoJsonDao;
import com.geostar.constructionland.analyse.entity.HttpResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResultDlMmj;
import com.geostar.constructionland.analyse.entity.model.AnalyseModel;
import com.geostar.constructionland.analyse.entity.resulthandler.HandlerResult;
import com.geostar.constructionland.analyse.factory.AdjustAgrFactory;
import com.geostar.constructionland.analyse.service.analyseresult.DlmmjService;
import com.geostar.constructionland.analyse.service.handler.IResultHandler;
import com.geostar.constructionland.analyse.service.handler.impl.util.XmlUtil;
import com.geostar.constructionland.common.entity.logs.mongolog.YzGeoJson;
import com.geostar.constructionland.common.global.ConstAnalyseType;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

/**
 * 地类毛面积分析
 * @author yell
 * @since 2020/07/29
 */
@Service(value = ConstAnalyseType.CONST_DLMJJFX+"Ver2.0")
@Slf4j
public class DlMjjHandlerVer2 implements IResultHandler {
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    DlmmjService dlmmjService;
    @Autowired
    YzGeoJsonDao yzGeoJsonDao;
    @Autowired
    AdjustAgrFactory adjustAgrFactory;
    @Override
    public HandlerResult handler(AnalyseModel analyseModel, HttpResult httpResult) throws Exception {
        HandlerResult result = HandlerResult.createBySuccess();
        log.info("地类毛面积返回数据,状态码:{}",httpResult.getCode());
        if(HttpStatus.SC_OK==httpResult.getCode()){
            String newBody = XmlUtil.parseXml(httpResult.getBody());
            httpResult.setBody(newBody);
            /**保存占压信息**/
            AlyResult alyResult = objectMapper.readValue(httpResult.getBody(),AlyResult.class);
            log.info("body:"+httpResult.getBody());
            if(HttpStatus.SC_OK == alyResult.getStatus()){
                /**如果成功,构建主体解析**/
                List<AlyResultDlMmj> alyResultDlMmjs = objectMapper.readValue(alyResult.getAnalysisResult().getResultRecords(),
                        new TypeReference<List<AlyResultDlMmj>>(){});
                alyResultDlMmjs.forEach(alyResultDlMmj -> {
                    YzGeoJson yzGeoJson = new YzGeoJson();
                    String yzGeoJsonId = UUID.randomUUID().toString().replaceAll("-","");
                    yzGeoJson.setId(yzGeoJsonId);
                    yzGeoJson.setGeoJson(alyResultDlMmj.getGeojson());
                    yzGeoJsonDao.saveYzGeoJson(yzGeoJson);
                    alyResultDlMmj.setTbmj(new BigDecimal(alyResultDlMmj.getTbmjYs()).setScale(0,BigDecimal.ROUND_HALF_UP).doubleValue());
                    alyResultDlMmj.setMongoId(yzGeoJsonId);
                    alyResultDlMmj.setFxbs(analyseModel.getAlyId());
                });
                dlmmjService.saveBatch(alyResultDlMmjs);
            }else{
                throw new Exception(alyResult.getMessage());
            }
        }
        return result;
    }
}
